package org.lee.streams;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

import static java.util.Comparator.comparingLong;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

/**
 * @author jackielee
 * @version 1.0
 * @description Demo1
 * @date 2021/4/16
 */
public class Demo1 {
  public static void main(String[] args) {

    List<Apple> appleList = new ArrayList<>();
    appleList.add(new Apple(1, "苹果1", new BigDecimal("3.52"), 10));
    appleList.add(new Apple(2, "香蕉1", new BigDecimal("5.21"), 8));
    appleList.add(new Apple(3, "荔枝1", new BigDecimal("3.81"), 92));
    appleList.add(new Apple(3, "荔枝2", new BigDecimal("4.92"), 45));
    appleList.add(new Apple(2, "香蕉2", new BigDecimal("8.15"), 32));
    appleList.add(new Apple(1, "苹果6", new BigDecimal("3.98"), 5));

    List<Apple> unique =
        appleList.stream()
            .collect(
                collectingAndThen(
                    toCollection(() -> new TreeSet<>(comparingLong(Apple::getId))),
                    ArrayList::new));
    System.out.println(unique);
  }
}
